package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
)

type Devicel struct {
	XMLName xml.Name `xml:"devices"`
	Version string   `xml:"version,attr"`
	Host    []Hostl  `xml:"host"`
	Desc    string   `xml:",innerxml"`
	Comment string   `xml:",comment"`  // 反序列化的时候用，生成注释<!--这是注释-->
	Data    string   `xml:",chardata"` // 反序列化用，和innerxml相反，

}
type Hostl struct {
	XMLName  xml.Name `xml:"host"`
	HostName string   `xml:"hostName"`
	HostCode string   `xml:"hostCode"`
	HostDate string   `xml:"hostDate"`
	ID       int      `xml:"id,attr"`
}

func main() {
	var d Devicel = Devicel{Comment: "这是注释", Data: "<host><Name>测试</Name></host>"}
	d.Host = append(d.Host, Hostl{HostName: "订单服务", HostCode: "1001", HostDate: "2024-09-08", ID: 1})
	d.Host = append(d.Host, Hostl{HostName: "数据服务", HostCode: "1002", HostDate: "2024-09-08"})
	d.Host = append(d.Host, Hostl{HostName: "商品服务", HostCode: "1003", HostDate: "2024-09-08"})
	data, err := xml.Marshal(d)
	if err != nil {
		fmt.Println("出错：", err)
		return
	}
	fmt.Println(string(data))
	// 1 不带xml头的
	//ioutil.WriteFile("./device2.xml",data,0666)
	// 2 带xml头的
	headByte := []byte(xml.Header)       // 把xml头转成byte切片
	headByte = append(headByte, data...) // 把数据拼到后面
	ioutil.WriteFile("./device2.xml", headByte, 0666)
}
